﻿//
// (C) Copyright 2009 Irantha Suwandarathna (irantha@gmail.com)
// All rights reserved.
//

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.IO.Compression;

namespace EffiProzDB.Lib
{
    /**
 *
 * New Class based on original Hypersonic code.
 *
 * @author Thomas Mueller (Hypersonic SQL Group)
 * @version 1.7.2
 * @since Hypersonic SQL
 */
public class ZipUnzipFile {

    private static int COPY_BLOCK_SIZE = 1 << 16;

    public static void compressFile(string infilename, string outfilename,
                                    HSQLFileAccess storage) {

        Stream       _in        = null;
        GZipStream   f         = null;
        bool      completed = false;

        // if there is no file
        if (!storage.IsStreamElement(infilename)) {
            return;
        }

        try {
            byte[] b = new byte[COPY_BLOCK_SIZE];

            _in = storage.OpenInputStreamElement(infilename);
            f = new GZipStream(
                storage.OpenOutputStreamElement(outfilename),
                CompressionMode.Compress);

            while (true) {
                int l = _in.Read(b, 0, COPY_BLOCK_SIZE);

                if (l == 0) {
                    break;
                }

                f.Write(b, 0, l);
            }

            completed = true;
        } catch (Exception e) {
            throw FileUtil.toIOException(e);
        } finally {
            try {
                if (_in != null) {
                    _in.Close();
                }

                if (f != null) {
                    f.Flush();    // reported to be missing from close() in some JRE libs
                    f.Close();
                }

                if (!completed && storage.IsStreamElement(outfilename)) {
                    storage.RemoveElement(outfilename);
                }
            } catch (Exception e) {
                throw FileUtil.toIOException(e);
            }
        }
    }

    public static void decompressFile(string infilename, String outfilename,
                                      HSQLFileAccess storage)  {

        GZipStream f = null;
        Stream        outstream = null;
        bool            completed = false;

        try {
            if (!storage.IsStreamElement(infilename))
            {
                return;
            }

            storage.RemoveElement(outfilename);

            f = new GZipStream(
                storage.OpenInputStreamElement(infilename), CompressionMode.Decompress);
            outstream = storage.OpenOutputStreamElement(outfilename);

            byte[] b = new byte[COPY_BLOCK_SIZE];

            while (true) {
                int l = f.Read(b, 0, COPY_BLOCK_SIZE);

                if (l == 0) {
                    break;
                }

                outstream.Write(b, 0, l);
            }

            completed = true;
        } catch (Exception e) {
            throw FileUtil.toIOException(e);
        } finally {
            try {
                if (f != null) {
                    f.Close();
                }

                if (outstream != null) {
                    outstream.Flush();
                    outstream.Close();
                }

                if (!completed && storage.IsStreamElement(outfilename)) {
                    storage.RemoveElement(outfilename);
                }
            } catch (Exception e) {
                throw FileUtil.toIOException(e);
            }
        }
    }
}
}
